package com.alibaba.aone.casestudy.common.utils;
/**
 * 类TestUtil.java的实现描述：TODO 类实现描述 
 * @author yongchun.chengyc 2011-5-6 上午09:54:09
 */
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
public class MessageUtil {




	   /* public static void main(String[] args) {
	    	System.out.println("你好");
	        sendWangwangMessage("yongchun.chengyc",
	                            "忠华说：<br> 这是一个测试消息！ <a href=\" http://www.sample.com\" target=\"_blank\">sample link</a>");
	    }*/

	    public static void sendWangwangMessage(String accountId, String message) {
	        try {
	            URL u = new URL(" http://aone.alibaba-inc.com/perth/externalapi/externalapi.jsp"
	                            + "?action=resourceMapApi&accountId=" + accountId + "&msg=" + encode(message));
	            u.openStream();
	        } catch (Exception e) {
	            throw new RuntimeException(e);
	        }
	    }

	    private static String encode(String message) {
	        StringBuilder sb = new StringBuilder();
	        if (message == null) {
	            sb.append("[null]");
	        } else {
	            for (int i = 0; i < message.length(); i++) {
	                char c = message.charAt(i);
	                if (c > 255) {
	                    sb.append("&#");
	                    sb.append((int) c);
	                    sb.append(";");
	                } else {
	                    sb.append(c);
	                }
	            }
	        }
	        try {
	            return URLEncoder.encode(URLEncoder.encode(sb.toString(), "UTF-8"), "UTF-8");
	        } catch (UnsupportedEncodingException e) {
	            throw new RuntimeException(e);
	        }
	    }
	}

	